//
//  HMPhotoBrowerViewController.swift
//  WeiBo
//
//  Created by apple on 16/7/6.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SVProgressHUD
let HMPhotoBrowerViewControllerReuseIdentifier = "HMPhotoBrowerViewControllerReuseIdentifier"

class HMPhotoBrowerViewController: UIViewController {

    private var pic_urls: [HMPhotoViewModel]?
    private var indexPath: NSIndexPath?
    
    // 对外创建一个构造方法,把数据传递过来
    init(pic_urls: [HMPhotoViewModel],indexPath: NSIndexPath){
        
        self.pic_urls = pic_urls
        self.indexPath = indexPath
        
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
        setupCollectionView()
        
    }

    // 添加视图和设置约束
    private func setupUI(){
        
        view.addSubview(collectionView)
        view.addSubview(closeButton)
        view.addSubview(saveButton)
        
        collectionView.frame = view.bounds
        
        closeButton.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(view.snp_left).offset(20)
            make.bottom.equalTo(view.snp_bottom).offset(-50)
            
            make.width.equalTo(100)
            make.height.equalTo(30)
            
        }
        
        saveButton.snp_makeConstraints { (make) -> Void in
            
            make.right.equalTo(view.snp_right).offset(-20)
            make.bottom.equalTo(view.snp_bottom).offset(-50)
            
            make.width.equalTo(100)
            make.height.equalTo(30)
            
        }
        
}
    
    // 设置UICollectionView的属性和数据源方法
    private func setupCollectionView(){
        
        collectionView.dataSource = self
        
        // 注册cell
        collectionView.registerClass(HMPhotoBrowerCollectionViewCell.self, forCellWithReuseIdentifier: HMPhotoBrowerViewControllerReuseIdentifier)
        
        // 设置分页滚动
        collectionView.pagingEnabled = true
        
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        
        
        // 设置布局
        flowLayout.itemSize = collectionView.bounds.size
        
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
        
        // 设置滚动方向
        flowLayout.scrollDirection = .Horizontal
        
    }
    
    //MARK:-- 按钮的点击方法
    @objc private func close(){
        
        dismissViewControllerAnimated(false) { () -> Void in
            
        }
    }
    
    @objc private func save(){
        
        // 利用collectionView的 可显示 cell 的数组,拿到对应的cell
        let visibleCell = collectionView.visibleCells()[0] as! HMPhotoBrowerCollectionViewCell
        
        visibleCell.saveImage()
        
        
        visibleCell.closure = {
        
            // 进行一个提示
            SVProgressHUD.showInfoWithStatus("保存成功")
            
        }
    }
    
    // 懒加载
    private lazy var flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    private lazy var collectionView: UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: self.flowLayout)
    
    // 关闭按钮
    
    private lazy var closeButton: UIButton = {
    
        let button = UIButton(type: UIButtonType.Custom)
        
        button.addTarget(self, action: "close", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setTitle("关闭", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(16)
        
        button.backgroundColor = UIColor.darkGrayColor()
        
        return button
    }()
    
    // 保存按钮
    private lazy var saveButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        button.addTarget(self, action: "save", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setTitle("保存", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(16)
        
        button.backgroundColor = UIColor.darkGrayColor()
        
        return button
    }()
}

extension HMPhotoBrowerViewController: UICollectionViewDataSource,HMPhotoBrowerCollectionViewCellDelegate {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(HMPhotoBrowerViewControllerReuseIdentifier, forIndexPath: indexPath) as! HMPhotoBrowerCollectionViewCell
        
//        cell.backgroundColor = RandomColor()
        cell.photoViewModel = pic_urls![indexPath.item]
        
        cell.delegate = self
        
        return cell
    }
    
    func tapImageClose() {
        
        close()
        
    }
    
}
